import router from '@system.router';
import UserApi from '../../api/userApi.js';
import prompt from '@system.prompt';

export default {
    data: {
        baseImageUrl: '',
        checkToken: false,
        no_login_msg: '',
        user: {},
        qr_value: '你好，鸿蒙！',
    },
    onInit() { //'common/temp/' //
        this.baseImageUrl = this.$app.$def.globalData.baseImageUrl;
        let user = this.$app.$def.globalData.user;
        //        console.log('get User from APP, User: ' + user.username);
        if (user) {
            if (user.id != null && user.id != undefined && user.id != 0) {
                let params = {
                    id: user.id,
                    token: this.$app.$def.globalData.token
                }
                UserApi.checkToken(params, (data) => {
                    if (data.checkToken) {
                        this.checkToken = data.checkToken;
                        this.user = user;
                    } else {
                        this.no_login_msg = '会话已过期/(ㄒoㄒ)/~~';
                    }
                });
            }

        } else {
            this.no_login_msg = "去登录，发现更多精彩(●'◡'●)";
        }
    },
    onShow() {
        if (this.checkToken) {
            this.user = this.$app.$def.globalData.user;
        }
    },
    goLogin() {
        router.push({
            uri: 'pages/login/login'
        });
    },
    showQrcode() {
        if (this.checkToken) {
            this.$element('qrcodeDialog').show()
        } else {
            this.$element('noLoginDialog').show()
        }
    },
    editUser() {
        if (this.checkToken) {
            router.push({
                uri: 'pages/userDetail/userDetail'
            });
        } else {
            this.$element('noLoginDialog').show()
        }
    },

    checkLogout(){
        this.$element('logoutDialog').show();
    },

    cancel(){
        this.$element('logoutDialog').close();
    },

    logout() {
        let that = this;
        UserApi.logout((data) => {
            this.$app.$def.globalData.user = {};
            this.user = {};
            router.replace({
                uri: 'pages/main/main'
            });
            prompt.showToast({
                message: data
            })
        });

    }
}
